package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户资源控制器
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyCommentService commentService;

    @PostMapping
    public JSONResult register(User user) {
        JSONResult jsonResult = new JSONResult();
        try {
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
    @PutMapping("{id}")
    public JSONResult update(User user) {
        JSONResult jsonResult = new JSONResult();
        try {
            userService.saveOrUpdate(user);
            //把更新后的员工重新传到页面
            jsonResult.setObj(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    @GetMapping("{authorId}/travels")
    public PageInfo queryTravels(TravelQueryObject qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        qo.setState(Travel.STATE_RELEASE);
        return travelService.list(qo);
    }
    @GetMapping("{authorId}")
    public User getAuthorInfo(@PathVariable("authorId") Long id){
        return userService.getAuthorInfoById(id);
    }

    @PostMapping("{currentUserId}")
    public JSONResult insertRelation(@PathVariable("currentUserId")Long id,Long followerId){
        userService.insertRelations(id,followerId);
        return new JSONResult();
    }

    /**
     * 获取某个用户的粉丝以及关注数量
     * @param userId
     * @return
     */
    @GetMapping
    public Map queryFansAndNums(Long userId){
        Map<String,Object> map = new HashMap<>();
        int nums =  userService.queryNums(userId);
        int fans =  userService.queryFans(userId);
        map.put("nums",nums);
        map.put("fans",fans);
        return map;
    }

    @DeleteMapping("{userId}")
    public JSONResult deleteRelation(@PathVariable Long userId,Long followerId){
        userService.deleteRelations(userId,followerId);
        return new JSONResult();
    }
    @GetMapping("/{userId}/{followerId}")
    public Boolean getRelation(@PathVariable Long userId,@PathVariable Long followerId){
       return userService.getIsRelation(userId,followerId);
    }

    //获取某个用户的点评
    @GetMapping("{userId}/comments")
    public PageInfo queryUserComments(StrategyCommentQueryObject qo){
        qo.setOrderBy("sc.createTime desc");
        return commentService.queryCommentsByUserId (qo);
    }
}
